/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jforum.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jforum.DAO.MessageDAO;
import jforum.domain.Message;
import jforum.util.DBConnector;

/**
 *
 * @author Вадим
 */
public class SearchAction implements Action {

    public String perform(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String keyword = request.getParameter("keyword");
        String author = request.getParameter("author");

        DBConnector conn=DBConnector.getInstance();
        MessageDAO dao = new MessageDAO(conn);
        List<Message> messages;

        if (keyword.equals("")) {
            if (author.equals("")) {
                request.getSession().setAttribute("errorMessage", "Параметры поиска не заданы");
                return ActionNames.error_page;
            }
            else {
                messages = dao.LoadForUser(author);
            }
        }
        else {
            if (author.equals("")) {
                messages = dao.LoadByKeyword(keyword);
            } else {
                messages = dao.LoadForUserAndKeyword(keyword, author);
            }
        }
        request.setAttribute("messages", messages);
        return ActionNames.search_res_page;
    }

}
